from utils.my_drf.viewsets import CustomTreeModelViewSet
from utils.my_drf.admin_auth import AdminJwtAuth, AdminPermission
from . import serializer, models
from django.core.cache import cache
from record.models import AdminRecord


class AdminArticleSortView(CustomTreeModelViewSet):
    authentication_classes = [AdminJwtAuth, ]
    permission_classes = [AdminPermission, ]

    queryset = models.Sort.objects.filter(is_delete=False, group=1).order_by('-ord', '-id')
    serializer_class = serializer.AdminArticleSortSerializer

    def create(self, request, *args, **kwargs):
        res = super().create(request, *args, **kwargs)
        AdminRecord.objects.create(group=1, detail='添加文章分类，内容：{}'.format(str(res.data.get('data'))),
                                   admin_id=request.user.get('id'))
        cache.delete('article_sort')
        return res

    def update(self, request, *args, **kwargs):
        res = super().update(request, *args, **kwargs)
        AdminRecord.objects.create(group=1, detail='修改文章分类，内容：{}'.format(str(res.data.get('data'))),
                                   admin_id=request.user.get('id'))
        cache.delete('article_sort')
        return res

    def destroy(self, request, *args, **kwargs):
        res = super().destroy(request, *args, **kwargs)
        AdminRecord.objects.create(group=1, detail='删除文章分类，id：{}'.format(str(res.data.get('data'))),
                                   admin_id=request.user.get('id'))
        cache.delete('article_sort')
        return res
